package com.andon.microcellsimulator.common.socket;

import com.andon.microcellsimulator.common.util.LogUtil;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.List;

/**
 * Created by admin on 17/12/4.
 */
class UdpServerThread extends Thread {

    private static final int UDP_SERVER_PORT = 5766;
    private static final int UDP_SEND_PORT = 5767;
    private static final int BUFF_LEN = 128;

    private static boolean SHUT_DOWN_FLAG = false;

    private List<String> mWhiteList;

    public UdpServerThread() {
    }

    public boolean isShutDown() {
        return SHUT_DOWN_FLAG;
    }

    public void cancel(boolean isInterrupted) {

        SHUT_DOWN_FLAG = true;

        if (isInterrupted) {
            if (!isInterrupted()) {
                interrupt();
            }
        }
    }

    public void setWhiteList(List<String> whiteList) {
        mWhiteList = whiteList;
    }

    @Override
    public void run() {

        DatagramSocket server = null;
        byte[] recvBuf = null;
        DatagramPacket recvPacket = null;
        byte[] sendBuf = null;
        DatagramPacket sendPacket = null;

        while (true) {

            if (SHUT_DOWN_FLAG) {
                break;
            }

            try {
                server = new DatagramSocket(UDP_SERVER_PORT);

                recvBuf = new byte[BUFF_LEN];

                recvPacket = new DatagramPacket(recvBuf, recvBuf.length);

                server.receive(recvPacket);

                byte[] data = recvPacket.getData();

                LogUtil.i("udp服务接收到数据:[" + new String(data) + "]");

                byte[] bodyLen = new byte[] { data[0], data[1], data[2], data[3] };

                int length = Integer.parseInt(new String(bodyLen));
                byte[] body = new byte[length];
                System.arraycopy(data, 4, body, 0, length);
                String deviceID = new String(body);

                if (mWhiteList != null && mWhiteList.contains(deviceID)) {
                    sendUdpPacket(recvPacket.getAddress(), "1");
                    LogUtil.i("cp设备[" + deviceID + "]建立udp连接 - " + recvPacket.getAddress().getHostAddress() + ":" + recvPacket.getPort());
                } else {
                    sendUdpPacket(recvPacket.getAddress(), "0");
                    LogUtil.i("cp设备[" + deviceID + "]未在白名单,udp连接失败 - " + recvPacket.getAddress().getHostAddress() + ":" + recvPacket.getPort());
                }

            } catch (IOException e) {
                LogUtil.e("udp server出现异常", e);
            } catch (NumberFormatException e) {
                LogUtil.e("data异常", e);
            } finally {
                if (server != null) {
                    server.close();
                }
            }
        }

    }

    private void sendUdpPacket(InetAddress addr, String content) throws IOException {
        DatagramSocket socket = new DatagramSocket();
        byte[] sendBuf = content.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, addr, UDP_SEND_PORT);
        socket.send(sendPacket);
        socket.close();
    }

}
